package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gemme.gemme.R;
import com.gemme.gemme.gwc.GwcBean2;
import com.gemme.gemme.gwc.GwcNewFragment;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.wode.ProductNewActivity;

import java.util.HashMap;

public class GwcSingleItemView extends RelativeLayout {
    RoundCheckBox checkbox;
    ImageView image;
    TextView name;
    TextView tv_price;
    PriceView priceview;
    TextView tv_guige;
    ImageView delete;
    TextView btn_delete;

    private Context context;
    private GwcBean2.DataBean.RecordsBean listBean;
    private GwcNewFragment gwcNewFragment;
    public GwcSingleItemView(Context context) {
        this(context, null);
    }
    public GwcSingleItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GwcSingleItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.gwc_single_item, this);
        View ll_content = findViewById(R.id.ll_content);
        checkbox = findViewById(R.id.checkbox);
        image = findViewById(R.id.image);
        name = findViewById(R.id.name);
        tv_price = findViewById(R.id.tv_price);
        priceview = findViewById(R.id.priceview);
        tv_guige = findViewById(R.id.tv_guige);
        delete = findViewById(R.id.delete);
        btn_delete = findViewById(R.id.btn_delete);

        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ProductNewActivity.class);
                intent.putExtra("id", listBean.getProductId() + 0L);
                intent.putExtra("brandId", listBean.getBrandId() + 0L);
                context.startActivity(intent);
            }
        });
        name.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ProductNewActivity.class);
                intent.putExtra("id", listBean.getProductId() + 0L);
                intent.putExtra("brandId", listBean.getBrandId() + 0L);
                context.startActivity(intent);
            }
        });
        ll_content.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ProductNewActivity.class);
                intent.putExtra("id", listBean.getProductId() + 0L);
                intent.putExtra("brandId", listBean.getBrandId() + 0L);
                context.startActivity(intent);
            }
        });

        setNumberListener();

        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteGwc();
            }
        });

        btn_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteGwc();
            }
        });
    }

    private void deleteGwc() {
        String id = listBean.getId();
        if (TextUtils.isEmpty(id)) {
            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
            return;
        }

        String url = DataManager.BaseUrl + "mall/shopordercart/batchDeleteById?id=" + id;
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey, DataManager.getToken(context));
        DataManager.toServer2(context, url, DataManager.DELETE_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {

                    if (DataPares.isSucess(context, response)) {
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                    //删除后刷新购物车
                    gwcNewFragment.initData();
                } catch (Exception e) {

                }
                System.out.println("");
            }
        });
    }

    private void setNumberListener() {
        priceview.setOnchangeListener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                int num = priceview.getNum();
                listBean.setProductAmount(num);
            }
        });
    }

    public GwcSingleItemView setOnChangeLIstener(OnChangeLIstener onChangeLIstener) {
        checkbox.setOnChangeLIstener(onChangeLIstener);
        return this;
    }

    public RoundCheckBox getCheckbox() {
        return checkbox;
    }

    public void setData(GwcBean2.DataBean.RecordsBean listBean) {
        this.listBean = listBean;
        try {
            if(listBean.getPictureUrl()!=null){
                Glide.with(context).load(listBean.getPictureUrl()).into(image);
            }else {
                Glide.with(context).load(listBean.getProductUrl()).into(image);
            }
            name.setText(StrUtil.noNUll(listBean.getProductName()));
            tv_price.setText(StrUtil.noNUll(listBean.getPrice() + ""));
            priceview.setNum(listBean.getProductAmount());
            tv_guige.setText(StrUtil.noNUll(listBean.getSpecValue()) + "");
        } catch (Exception e) {

        }
    }

    public void setSingleDeleteIsVisible(boolean isEdit) {
        delete.setVisibility(isEdit == true ? VISIBLE : GONE);
    }

    public void setFragment(GwcNewFragment gwcNewFragment) {

        this.gwcNewFragment = gwcNewFragment;
    }
}
